package com.autonavi.yunda.yunji.core.vo;

import com.autonavi.yunda.yunji.common.enums.ContentType;
import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.common.utils.JsonUtils;
import com.autonavi.yunda.yunji.common.utils.http.HttpUtil;
import com.autonavi.yunda.yunji.common.utils.http.RequestParser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author cizhong.wcs
 * @date 2022/4/20 19:10
 */
@Data
@Builder
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class HttpRequest {
    private Map<String, String[]> parameterMap;
    private String body;
    private Map<String, String> headers;

    private MultiValueMap<String, MultipartFile> multiFileMap;

    private String uri;

    public HttpRequest paramStr(String paramStr) {
        parameterMap = HttpUtil.getParamsArrayMap(paramStr);
        return this;
    }

    public HttpRequest headers(Map<String, String> headers) {
        this.headers = headers;
        return this;
    }

    public HttpRequest(HttpServletRequest request, ContentType contentType) {
        parseBody(request, contentType);
        this.parameterMap = request.getParameterMap();
        this.headers = RequestParser.getHeaders(request);
        this.uri = request.getRequestURI();
    }

    public HttpRequest(Map<String, Object> args) {
        Map<String, String[]> parameterMap = new HashMap<>();
        args.forEach((k, v) -> parameterMap.put(k, new String[]{v.toString()}));
        this.parameterMap = parameterMap;
    }

    public String[] getParameterValues(String paramName) {
        if (parameterMap != null) {
            return parameterMap.get(paramName);
        }
        return null;
    }

    private void parseBody(HttpServletRequest request, ContentType contentType) {
        try {
            if (ContentType.MULTIPART_FORM.equals(contentType)) {
                this.multiFileMap = ((MultipartHttpServletRequest) request).getMultiFileMap();
                if (!request.getParameterMap().isEmpty()) {
                    this.body = JsonUtils.toString(request.getParameterMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()[0])));
                }
            } else if (ContentType.FORM_URLENCODED.equals(contentType)) {
                String body = IOUtils.toString(request.getReader());
                this.body = JsonUtils.toString(HttpUtil.getParamsMapUrlDecode(body));
            } else {
                this.body = IOUtils.toString(request.getReader());
            }
        } catch (IOException e) {
            throw new AmapException(AmapExceptionCode.COMMON_SERVER_ERROR);
        }
    }

    /**
     * job参数解决类似于 接口参数解析body
     */
    public static HttpRequest fromJopParam(String jopParam) {
        HttpRequest request = new HttpRequest();
        if (StringUtils.isBlank(jopParam)) {
            return request;
        }
        request.body = jopParam;
        return request;
    }
}
